<?

function slotQuery($data, $form, $json)
{
    if ($form['form_pitch'] == 'All') {
        $flag = true;
        $q = new WP_Query(array('post_type' => 'pitch', 'order' => 'ASC', 'posts_per_page' => 100000));
        while ($q->have_posts()) : $q->the_post();
            if (getVenueID($q->post->ID) == $form['form_venue']) {
                $data["pitch"] = $q->post->ID;
                $response = doesSlotConflict($data);
                if ($response['conflict']) {
                    $flag = false;
                    $json['status'] .= $response['status'];
                    array_push($json['deniedSlots'], $data);
                }
            }
        endwhile;

        if ($flag) {
            $q = new WP_Query(array('post_type' => 'pitch', 'order' => 'ASC', 'posts_per_page' => 100000));
            while ($q->have_posts()) : $q->the_post();
                if (getVenueID($q->post->ID) == $form['form_venue']) {
                    $data["pitch"] = $q->post->ID;
                    newSlot($data);
                    array_push($json['newSlots'], $data);
                    $json['status'] .= 'added slot for ' . $q->post->ID . ' ';
                }
            endwhile;
            wp_reset_postdata();
        }

    } else {
        $response = doesSlotConflict($data);
        if (!$response['conflict']) {
            newSlot($data);
            array_push($json['newSlots'], $data);
        } else {
            array_push($json['deniedSlots'], $data);
        }
        $json['status'] .= $response['status'];
    }

    return $json;
}

function newSlot($data)
{
    $array = array();

    $days = '';

    foreach ($data['day'] as $value) {
        $days = $days . $value . ', ';
    }

    $title = get_field('pid', $data['pitch'])
        . ', '
        . get_field('name', getVenueID($data['pitch']))
        . ' | '
        . $data['start_time']
        . ' - '
        . $data['end_time']
        . ' - '
        . $days;

    // Create post object
    $postOptions = array(
        'post_title' => $title,
        'post_type' => 'slot',
        'post_status' => 'publish',
        'post_author' => 1,
    );

    // Insert the post into the database
    $post_id = wp_insert_post($postOptions, $wp_error);

    wp_update_post(
        array(
            'ID' => $post_id,
            'post_name' => $post_id,
            'post_title' => '[Slot ' . $post_id . '] ' . $title
        )
        , $wp_error
    );

    update_field('field_50b4930d3ea03', $data['pitch'], $post_id);
    update_field('field_50b493223ea04', $data['start_time'], $post_id);
    update_field('field_50b493a83ea05', $data['end_time'], $post_id);
    update_field('field_50b495d51e7be', $data['day'], $post_id);
    update_field('field_50b494bd55bae', $data['price'], $post_id);
    update_field('field_518e0a12a383d', $data['active'], $post_id);

    $array['status'] = 'added post' . $post_id;
    $array['options'] = $data;
    $array['error'] = $wp_error;

    return $array;
}

function doesSlotConflict($data)
{
    $array = array();
    $array['status'] = '';
    $array['conflict'] = false;

    $q = new WP_Query(array('post_type' => 'slot', 'posts_per_page' => 100000));
    while ($q->have_posts()) : $q->the_post();

        if ($data['pitch'] == getPitchID($q->post->ID)) {
            $fields = get_fields($q->post->ID);

            $flag = false;
            try {
                $c = array_intersect($fields['day'], $data['day']);
            } catch (Exception $e) {
                $array['error'] = $e->getMessage();
            }

            if (count($c) > 0) {
                if ($data['start_time'] >= $fields['start_time'] && $data['start_time'] < $fields['end_time']) {
                    $flag = true;
                }
                if ($data['end_time'] > $fields['start_time'] && $data['end_time'] <= $fields['end_time']) {
                    $flag = true;
                }
                if ($fields['start_time'] >= $data['start_time'] && $fields['start_time'] < $data['end_time']) {
                    $flag = true;
                }
                if ($fields['end_time'] > $data['start_time'] && $fields['end_time'] <= $data['end_time']) {
                    $flag = true;
                }
                if ($flag) {
                    //$array['status'] .= ' error conflict found with ' . $q->post->ID . ' ' . $fields['start_time'] . '-' . $fields['end_time'] . ' and ' . $data['start_time'] . '-' . $data['end_time'] . ' | ';

                    $array['conflict'] = $flag;
                    //print_r($fields);
                } else {
                    //$array['status'] .= ' no conflict found with ' . $q->post->ID . ' ' . $fields['start_time'] . '-' . $fields['end_time'] . ' and ' . $data['start_time'] . '-' . $data['end_time'] . ' | ';
                }
            }
        }
    endwhile;

    return $array;
}